package com.sc.nft.dao;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sc.nft.dto.PayOrderPageDTO;
import com.sc.nft.entity.PayOrder;
import com.sc.nft.entity.dto.TripartitePayRefundListDTO;
import com.sc.nft.entity.vo.PayOrderVO;
import com.sc.nft.enums.PayChannelEnum;
import com.sc.nft.enums.PayMethodEnum;
import com.sc.nft.enums.PayStatusEnum;
import com.sc.nft.enums.PayTypeEnum;
import com.sc.nft.mapper.PayOrderMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;
import java.util.List;

/**
 * 支付轨迹表(sc_pay_order)数据DAO
 *
 * @author kancy
 * @since 2022-06-15 01:50:45
 * @description 由 Mybatisplus Code Generator 创建
 */
@Slf4j
@Repository
public class PayOrderDao extends ServiceImpl<PayOrderMapper, PayOrder> {


    public PayOrder getPaySuccessByOrderNo(String orderNo){
        LambdaQueryWrapper<PayOrder> wrapper =new LambdaQueryWrapper();
        wrapper.eq(PayOrder::getOrderNo,orderNo);
//        wrapper.eq(PayOrder::getPayType, PayTypeEnum.PAY);
        wrapper.eq(PayOrder::getStatus, PayStatusEnum.SUCCESS);
        wrapper.last("limit 1");
        return getOne(wrapper);
    }


    public PayOrder getByTradeNo(String tradeNo){
        LambdaQueryWrapper<PayOrder> wrapper =new LambdaQueryWrapper();
        wrapper.eq(PayOrder::getTradeNo,tradeNo);
        return getOne(wrapper);
    }
    public PayOrder getByOrderNo(String orderNo){
        LambdaQueryWrapper<PayOrder> wrapper =new LambdaQueryWrapper();
        wrapper.eq(PayOrder::getOrderNo,orderNo);
        wrapper.last(" order by id desc limit 1");
        return getOne(wrapper);
    }


    public String getPayAmount(Date startTime,Date endTime,PayTypeEnum orderType){
        QueryWrapper<PayOrder> wrapper =new QueryWrapper();
        wrapper.select("sum(ifnull(pay_amount,0)) as pay_amount");
        if(orderType==PayTypeEnum.PAY){
            wrapper.in("order_type",PayTypeEnum.PAY,PayTypeEnum.SECOND,PayTypeEnum.PAY_SALVAGE);
        }else{
            wrapper.eq("order_type",orderType);
        }
        wrapper.ne("pay_type",6);
        wrapper.eq("status",PayStatusEnum.SUCCESS.getCode());
        wrapper.between("pay_time",startTime,endTime);
        PayOrder payOrder = getOne(wrapper);
        if(ObjectUtil.isNull(payOrder)){
            return "0.00";
        }
        return payOrder.getPayAmount().setScale(2, RoundingMode.HALF_UP).toPlainString();
    }

    public BigDecimal getSumPayAmount() {
        return baseMapper.getSumPayAmount();
    }

    public BigDecimal getToSumPayAmount() {
        return baseMapper.getToSumPayAmount();
    }

    public BigDecimal getToSumPayAmountByDay(String day) {
        return baseMapper.getToSumPayAmountByDay(day);
    }

    public BigDecimal getPayAmount(Long userId) {
        return baseMapper.getPayAmount(userId);
    }

    public void updateWaitResult(String body, String result, Long id) {
        LambdaUpdateWrapper<PayOrder> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(PayOrder::getReqResult,body);
        wrapper.set(PayOrder::getPayResult,result);
        wrapper.eq(PayOrder::getId,id);
        wrapper.eq(PayOrder::getStatus,PayStatusEnum.INIT);
    }

    /**
     * 支付轨迹表分页
     *
     * @param orderNo      订单编号
     * @param tradeNo      交易订单号
     * @param userTel      手机号
     * @param payType      支付类方式
     * @param channel      支付渠道
     * @param orderType    订单类型
     * @param bankCardNo   银行卡号
     * @param status       状态
     * @param startPayTime 开始支付时间
     * @param endPayTime   结束支付时间
     * @return
     */
    public Page<PayOrderPageDTO> payOrderPageFromAdmin(String orderNo, String tradeNo, String userTel, PayMethodEnum payType,
                                                       PayChannelEnum channel, PayTypeEnum orderType, String bankCardNo,
                                                       PayStatusEnum status, Date startPayTime,
                                                       Date endPayTime, int pageNo, int pageSize) {
        return baseMapper.payOrderPageFromAdmin(new Page(pageNo,pageSize),orderNo,tradeNo,userTel,payType,channel,orderType,bankCardNo,status,startPayTime,endPayTime);
    }
    public int payUserCount(Date startTime,Date endTime){
        return baseMapper.payUserCount(startTime, endTime);
    }

    /**
     *
     * @param tripartitePayRefundListDTO
     * @return
     */
    public List<PayOrderVO> tripartitePayRefundList(TripartitePayRefundListDTO tripartitePayRefundListDTO) {
        return baseMapper.tripartitePayRefundList(tripartitePayRefundListDTO);
    }


    /**
     * 获取国库入金
     * @param userId
     * @return
     */
    public BigDecimal getNationalTreasuryEnterMoney(Long userId, Date startTime, Date endTime) {
        return baseMapper.getNationalTreasuryEnterMoney(userId, startTime, endTime);
    }



}